/**
 * @(#) OverView.as
 *
 * @date		2009. 04. 20.
 * @version		1.0
 * @author		Jo, ChangHun
 * @description	연말정산 메인 view - Mediator
 * 				연말정산 - 탭6 특별공제 보험료 팝업 Presentation Logic을 관리 
 */
package com.skt.common_hr.yearadjustment.view
{
	import com.skt.common_hr.common.PsHrAlert;
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.yearadjustment.YearAdjustmentFacade;
	import com.skt.common_hr.yearadjustment.model.PremiumProxy;
	import com.skt.common_hr.yearadjustment.model.vo.PremiumVO;
	import com.skt.common_hr.yearadjustment.view.components.popPremiumSubtraction;
	import com.wcl.utils.StringUtil;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.managers.PopUpManager;
	
	import org.puremvc.as3.multicore.interfaces.INotification;
	import org.puremvc.as3.multicore.patterns.mediator.Mediator;
	

	public class popPremiumMediator extends Mediator
	{
		public static const NAME:String = "popPremiumMediator"; 
		public var v_year:String = "";
		[Bindable] public var v_enable:Boolean = true;


		/**
		* 생성자
		* 
		* @param
		* @return void
		*/
        public function popPremiumMediator( viewComponent:Object )
        {
            super( NAME, viewComponent );
            view.callLater(initView);
        }
 		
		/**
		* View를 초기화 한다.
		*	- View에 기본사항을 출력하고 data를 load
		* @param
		* @return void
		*/
 		public function initView():void
 		{
 			view.addEventListener(popPremiumSubtraction.OkInput, onOkInput);
 			view.addEventListener(popPremiumSubtraction.CancelInput, onCancelInput);
 			view._isEnable = v_enable;
 			//familyListSelect();
 			premiumListSelect();
 		}
 		
 		
 		private function premiumListSelect():void{
 			var proxy:PremiumProxy = facade.retrieveProxy(PremiumProxy.NAME) as PremiumProxy;
 			proxy.load(Variables.yearPernr, v_year);
 		}
 		
 		private function onOkInput(event:Event):void{
 			
 		 	var proxy:PremiumProxy = facade.retrieveProxy(PremiumProxy.NAME) as PremiumProxy;
 			
 			var detailList:Array = new Array();
	 		for each(var rec:PremiumVO in view.premiumData)
	 		{
	 			detailList.push(rec);
	 		}
 			proxy.save(Variables.yearPernr, v_year, detailList);
 		}
 		
 		private function getArrData(arr:ArrayCollection, seqNo:String):Object{
 			var returlArr:ArrayCollection = new ArrayCollection();
 			var returnObj:Object = new Object();
 			for(var i:int = 0; i < arr.length; i++){
 				if(StringUtil.trim(arr[i].SEQNO) == seqNo){
 					returnObj = arr[i];
 					return returnObj;
 				}
 			}
 			return null;
 		}
 		
 		private function onCancelInput(event:Event):void
 		{
 			facade.removeMediator(popPremiumMediator.NAME);
 			PopUpManager.removePopUp(view);
 		}
 		
 		
 		

		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Process MVC Messages
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		* 본 Class에서 처리해야할 Noti들을 등록한다.
		* 
		* @param
		* @return void
		*/
		override public function listNotificationInterests():Array 
		{
			return [
					/* YearAdjustmentFacade.LOAD_PRE_FAMILY_COMPLETE,
					YearAdjustmentFacade.LOAD_PRE_FAMILY_FAILED, */
					YearAdjustmentFacade.LOAD_PREMIUM_LIST_COMPLETE,
					YearAdjustmentFacade.LOAD_PREMIUM_LIST_FAILED,
					YearAdjustmentFacade.SAVE_PREMIUM_LIST_COMPLETE,
					YearAdjustmentFacade.SAVE_PREMIUM_LIST_COMPLETE,
					YearAdjustmentFacade.SAVE_PREMIUM_LIST_FAILED
					];
		}
		

		/**
		* Noti별로 처리한다.
		* 
		* @param
		* @return void
		*/
		override public function handleNotification(note:INotification):void
		{
			switch(note.getName()) 
			{
				/* case YearAdjustmentFacade.LOAD_PRE_FAMILY_COMPLETE:
					trace("YearAdjustmentFacade.LOAD_PRE_FAMILY_COMPLETE");
					view.familyInfo = (note.getBody() as PremiumProxy).familyInfo;
					premiumListSelect();
					break;
				case YearAdjustmentFacade.LOAD_PRE_FAMILY_FAILED:
					trace("YearAdjustmentFacade.LOAD_PRE_FAMILY_FAILED");
					break; */
				case YearAdjustmentFacade.LOAD_PREMIUM_LIST_COMPLETE:
					trace("YearAdjustmentFacade.LOAD_PREMIUM_LIST_COMPLETE");
//shgoo 2010.01.07					
					//view.premiumData = (note.getBody() as PremiumProxy).premiumData;
					view.premiumData = getValidInhabitants((note.getBody() as PremiumProxy).premiumData);
					break;
				case YearAdjustmentFacade.LOAD_PREMIUM_LIST_FAILED:
					trace("YearAdjustmentFacade.LOAD_PREMIUM_LIST_FAILED");
					break;
				case YearAdjustmentFacade.SAVE_PREMIUM_LIST_COMPLETE:
					trace("YearAdjustmentFacade.SAVE_PREMIUM_LIST_COMPLETE");
					//regMediator();
					facade.sendNotification(YearAdjustmentFacade.RELOAD_BASIC_DATA);
					onCancelInput(null);
					//facade.sendNotification(YearAdjustmentView.CHECK_REQ_PREMIUMSUBTRACTION);
					break;
				case YearAdjustmentFacade.SAVE_PREMIUM_LIST_FAILED:
					PsHrAlert.showMessage((note.getBody() as PremiumProxy).msgStr);
					trace("YearAdjustmentFacade.SAVE_PREMIUM_LIST_FAILED");
					break;	
				default: 
					//trace(this.instanceID+"에서 처리 안된 Noti가 검출됨 : " + note.getBody());
					break;
			}
		}

//shgoo 2010.01.07		
		//대상자 주민번호 추출
		private function getValidInhabitants(arrSrc:ArrayCollection):ArrayCollection {
			var arrDest:ArrayCollection = new ArrayCollection();
			
			for each ( var rec:PremiumVO in arrSrc) {
				if(rec.DPTID == "X" || (rec.DPTYP=="" && rec.SUBTY=="")) {
					
					arrDest.addItem(rec);
				}
			}
			
			if(arrDest.length<1) {
				//arrDest.addItem(new PremiumVO());
			}
			return arrDest;
		}
///////////////////
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Utility Functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
        private function get view():popPremiumSubtraction
        {
            return viewComponent as popPremiumSubtraction;
        }
	}
}